<template>
  <div class="user-container">
    <div class="list-container">
      <div class="filter">
        <el-row :gutter="20">
          <el-col :span="10">
            <el-select
              size="small"
              @change="userchange"
              v-model="value"
              placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-col>
          <el-col :span="13" style="text-align: right;">
            <el-button style="width:125px;" v-if="listState == 2" type="success" size="small" @click="createClick('insert')">批量设置岗位人员</el-button>
            <el-button style="width:125px;" v-if="listState == 1" type="danger" size="small" @click="handleDelete('delete')">批量撤销岗位人员</el-button>
          </el-col>
        </el-row>
      </div>
      <div class="data-contant">
        <div>
        <el-table
          :key='tableKey'
          :data="list.records"
          v-loading="loading"
          height="280px"
          element-loading-text="给我一点时间"
          size="small"
          stripe
          style="width: 100%;border:#eeeeee"
          @selection-change="handleSelectionChange"
          highlight-current-row
          v-if="listState == 1">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            align="center"
            type="index"
            width="80"
            label="序号"
          >
            <template slot-scope="scope">
            <span>{{scope.$index + (serialNumber - 1) * postList.rowBounds.limit + 1}}</span>
            </template>
          </el-table-column>
          <!--<el-table-column
            align="center"
            label="用户名称"
            prop="user_name "
          >
          </el-table-column>-->

          <el-table-column
            align="center"
            label="登录名"
            prop="user_loginName"
          >
          </el-table-column>
          <el-table-column
            align="center"
            label="用户名称"
            prop="user_name"
          >
          </el-table-column>
          <!--<el-table-column
            align="center"
            label="性别"
            :formatter="fmtGender"
            prop="user_gender"
          >
          </el-table-column>-->
          <el-table-column
            align="center"
            label="状态"
            :formatter="fmtLength"
            prop="user_status"
          >
          </el-table-column>
          <el-table-column align="center" label="操 作" width="220px" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button style="width:100px;" v-if="listState == 2" type="success" size="small" @click="createoneClick(scope.row,'insert')">设置岗位人员</el-button>
              <el-button style="width:100px;" v-if="listState == 1" type="danger" size="small" @click="handleoneDelete(scope.row,'delete')">撤销岗位人员</el-button>
            </template>
          </el-table-column>
        </el-table>
          <el-table
            :key='tableKey'
            :data="list.records"
            v-loading="loading"
            element-loading-text="给我一点时间"
            size="small"
            height="280px"
            stripe
            style="width: 100%;border:#eeeeee"
            @selection-change="handleSelectionChange"
            highlight-current-row
            v-if="listState == 2">
            <el-table-column
              type="selection"
              width="55">
            </el-table-column>
            <el-table-column
              align="center"
              type="index"
              width="80"
              label="序号"
            >
            <template slot-scope="scope">
            <span>{{scope.$index + (serialNumber - 1) * postList.rowBounds.limit + 1}}</span>
            </template>
            </el-table-column>
            <!--<el-table-column
              align="center"
              label="用户名称"
              prop="user_name "
            >
            </el-table-column>-->

            <el-table-column
              align="center"
              label="登录名"
              prop="loginName"
            >
            </el-table-column>
            <el-table-column
              align="center"
              label="用户名称"
              prop="name"
            >
            </el-table-column>
            <!--<el-table-column
              align="center"
              label="性别"
              :formatter="fmtGender"
              prop="user_gender"
            >
            </el-table-column>-->
            <el-table-column
              align="center"
              label="状态"
              :formatter="fmtLength"
              prop="status"
            >
            </el-table-column>
            <el-table-column align="center" label="操 作" width="220px" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button style="width:100px;" v-if="listState == 2" type="success" size="small" @click="createoneClick(scope.row,'insert')">设置岗位人员</el-button>
                <el-button style="width:100px;" v-if="listState == 1" type="danger" size="small" @click="handleoneDelete(scope.row,'delete')">撤销岗位人员</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-container">
            <el-pagination
              layout="total, sizes, prev, pager, next, jumper"
              :page-size = "this.postList.rowBounds.limit"
              :total="this.list.count"
              @size-change="handleSizeChange"
              @current-change="current_change"
            >
            </el-pagination>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import '@/styles/newstyle.scss'
  const nameCondition = {
    'property': 'name',
    'value': '',
    'type': 'LIKE'
  }
  const ennameCondition = {
    'property': 'nameEn',
    'value': '',
    'type': 'LIKE'
  }
  export default {
    data() {
      return {
        dappendVisible: false,
        updateVisible: false,
        tableKey: 0,
        list: {},
        postList: {
          condition: [],
          rowBounds: {
            offset: 0,
            limit: 10
          }
        },
        loading: false,
        fliter: [
          Object.assign({}, nameCondition),
          Object.assign({}, ennameCondition)
        ],
        appendlist: [],
        options: [
          {
            value: 1,
            label: '已设置人员'
          },
          {
            value: 2,
            label: '未设置人员'
          }
        ],
        value: 1,
        listState: 1,
        updatelist: [],
        deletelist: [],
        updateonelist: [],
        appendonelist: [],
        multipleUnSelection:[],
        multipleSelection:[],
        serialNumber:1
      }
    },
    props: ['JobUserData'],
    created() {
      this.fetchExistUser()
    },
    watch: {
    },
    methods: {
      fetchExistUser(flag) {
        this.loading = true
        this.postList.orgId = this.JobUserData.orgId
        this.postList.groupId = this.JobUserData.groupId
        if (flag == 'delete') {
          if ((this.postList.rowBounds.offset == (this.list.count-1)
           ||  this.list.records.length == this.multipleSelection.length)
           && this.postList.rowBounds.offset >= this.postList.rowBounds.limit) {
            if (this.serialNumber * this.postList.rowBounds.limit >= this.list.count) {
              this.postList.rowBounds.offset -=this.postList.rowBounds.limit
              this.serialNumber -=1
            }
          }
        }
        this.$store.dispatch('SelectExistGroupUser', this.postList).then(res => {
          this.list = res.data
          this.listState = 1
          this.loading = false
        }).catch((err) => {
          console.log(err)
        })
      },
      fetchUnExistUser(flag) {
        this.loading = true
        this.postList.orgId = this.JobUserData.orgId
        this.postList.groupId = this.JobUserData.groupId
        if (flag == 'insert') {
          if ((this.postList.rowBounds.offset == (this.list.count-1)
           ||  this.list.records.length == this.multipleUnSelection.length)
           && this.postList.rowBounds.offset >= this.postList.rowBounds.limit) {

            if (this.serialNumber * this.postList.rowBounds.limit >= this.list.count) {
              this.postList.rowBounds.offset -=this.postList.rowBounds.limit
              this.serialNumber -=1
            }
          }
        }
        this.$store.dispatch('SelectNoExistGroupUser', this.postList).then(res => {
          this.list = res.data
          this.listState = 2
          this.loading = false
        }).catch((err) => {
          console.log(err)
        })
      },
      appendData(flag) {
        this.$store.dispatch('GroupUserbatch', this.appendlist).then(res => {
          this.fetchUnExistUser(flag)
        }).catch((err) => {
          console.log(err)
        })
      },
      deleteData(flag) {
        this.$store.dispatch('GroupUserForceBatch', this.deletelist).then(res => {
          this.fetchExistUser(flag)
        }).catch((err) => {
          console.log(err)
        })
        this.deletelist = []
      },
      appendoneData(flag) {
        this.$store.dispatch('GroupUserbatch', this.appendonelist).then(res => {
          this.fetchUnExistUser(flag)
        }).catch((err) => {
          console.log(err)
        })
      },
      deleteoneData(flag) {
        this.$store.dispatch('GroupUserForceBatch', this.deleteonelist).then(res => {
          this.fetchExistUser(flag)
        }).catch((err) => {
          console.log(err)
        })
      },
      fmtLength(row, column) {
        const arr = row[column.property]
        /* 这里的 if(arr === undefined)
         * 是根据自己的需求，决定arr长度为0时的判断条件
         * 可以和这个不同
         */
        if (arr === '0') {
          return '活动'
        } else if (arr === '1') {
          return '禁用'
        } else if (arr === '2') {
          return '删除'
        }
      },
      fmtGender(row, column) {
        const arr = row[column.property]
        if (arr === '0') {
          return '未知'
        } else if (arr === '1') {
          return '男'
        } else if (arr === '2') {
          return '女'
        }
      },
      handleDelete(flag) {
        if (this.deletelist.length == 0) {
          this.$message({
            type: 'warning',
            message: '请选择撤销人员!'
          })
        } else {
          this.$confirm('是否撤销岗位人员?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            /* this.deletelist.userId = row.user_id
            this.deletelist.groupId = this.$route.params.groupId*/
            this.deleteData(flag)
            this.$message({
              type: 'success',
              message: '撤销成功!'
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        }
      },
      createClick(flag) {
        if (this.appendlist.length == 0) {
          this.$message({
            type: 'danger',
            message: '未选择岗位人员'
          })
        } else {
          this.$confirm('是否配置岗位人员?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.appendData(flag)
            this.$message({
              type: 'success',
              message: '配置管理员成功!'
            })
            this.appendlist = []
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消'
            })
          })
        }
      },
      handleoneDelete(row,flag) {
        this.$confirm('是否撤销岗位人员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteonelist = [
            {
              userId: row.user_id,
              groupId: this.JobUserData.groupId
            }
          ]
          this.deleteoneData(flag)
          this.$message({
            type: 'success',
            message: '撤销成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      createoneClick(row,flag) {
        this.$confirm('是否配置岗位人员?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          console.log(row);
          this.appendonelist = [
            {
              userId: row.id,
              groupId: this.JobUserData.groupId
            }
          ]
          this.appendoneData(flag)
          this.$message({
            type: 'success',
            message: '配置岗位成功!'
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
      },
      // 翻页器使用
      handleSizeChange(val) {
        this.postList.rowBounds.limit = val
        this.postList.rowBounds.offset = this.postList.rowBounds.limit * (this.serialNumber - 1)
        if (this.listState === 1) {
          this.fetchExistUser()
        } else {
          this.fetchUnExistUser()
        }
      },
      current_change(currentPage) { // 通过分页获取数据
        this.serialNumber = currentPage
        const page = currentPage - 1
        this.postList.rowBounds.offset = page * this.postList.rowBounds.limit
        if (this.listState === 1) {
          this.fetchExistUser()
        } else {
          this.fetchUnExistUser()
        }
      },
      /*
      *  查询
      * */
      userchange(val) {
        this.postList.rowBounds.offset = 0
        this.serialNumber = 1
        if (val === 1) {
          this.fetchExistUser()
        } else {
          this.fetchUnExistUser()
        }
      },
      title_change(type) { // 分类数据
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'name') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchExistUser()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'name') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchExistUser()
        }
      },
      entitle_change(type) { // 分类数据
        if (type === null) {
          const nullfliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].property === 'nameEn') {
              nullfliter.splice(i, 1)
              this.fliter[i].value = ''
            }
          }
          for (let i = 0; i < nullfliter.length; i++) {
            if (nullfliter[i].value === '') {
              continue
            }
            condition.push(nullfliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchExistUser()
        } else {
          const conditionTimefliter = this.fliter.concat()
          const condition = []
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].property === 'nameEn') {
              conditionTimefliter[i].value = type
              this.fliter[i].value = type
            }
          }
          for (let i = 0; i < conditionTimefliter.length; i++) {
            if (conditionTimefliter[i].value === '') {
              continue
            }
            condition.push(conditionTimefliter[i])
          }
          this.postList.condition = condition
          this.postList.rowBounds = {
            'offset': 0,
            'limit': 10
          }
          this.fetchExistUser()
        }
      },
      handleSelectionChange(val) {
        this.deletelist = []
        this.appendlist = []
        for (let i = 0; i < val.length; i++) {
          const ss = {
            groupId: this.JobUserData.groupId,
            userId: val[i].user_id?val[i].user_id:val[i].id
          }
          this.deletelist.push(ss)
          this.appendlist.push(ss)
        }
        if (this.listState === 1) {
          this.multipleSelection = val
        } else {
          this.multipleUnSelection = val
        }
      }
    }
  }
</script>

<style scoped>
  .user-container{
    height: 100%;
    overflow-y: auto;
  }
  .container{
    padding: 20px;
    height: 100%;
    overflow-y: auto;
  }
  .select-container{
    padding: 20px 10px 0;
    border-radius: 4px;
  }
  .list-container{
    margin-top:2px;
    padding:0 10px 10px 10px;
    border-radius: 4px;
  }
  .data-contant{
    padding: 30px 10px 20px;
  }
  .data-detail{
    font-size: 14px;
    padding:20px 0;
    text-align: center;
  }
  .data-title{
    font-size: 14px;
    padding:10px 0;
    text-align: center;
  }
  .data-title div{
    font-size: 14px;
    padding:5px 0;
  }
  .filter{
    padding: 10px 5px;
    border-bottom: 2px solid#e4e7ed;
  }
  .list-container{
  }
  .bold{
    font-weight: bold;
  }
</style>
